<?php

namespace App\Models;

class ActivityLog extends \Spatie\Activitylog\Models\Activity
{
    protected $casts = [
        'properties' => 'collection',
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];

    /**
     * 允许前端传长度
     * @return int
     */
    public function getPerPage(): int
    {
        $path = (string) app('request')->path();
        $isH5 = false !== strpos($path, 'h5');
        $h5WhiteList = ($path === 'api/h5/clients'); // h5 限制白名单
        $perPage = (int) (app('request')->input('per_page', null) ?: parent::getPerPage());

        return (($perPage > 15) && ($isH5 && !$h5WhiteList)) ? 15 : $perPage;
    }

   /**
     * 为数组 / JSON 序列化准备日期。
     *
     * @param  \DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

}
